﻿namespace ExamV5.ZiExam
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class JoinExam : Page
    {
        protected HtmlInputHidden EndTime;
        protected HtmlForm Form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private HttpCookie httpCookie_4 = null;
        protected System.Web.UI.WebControls.Image ImageUser;
        private int int_0 = 0;
        protected Label labDeptName;
        protected Label labDoc;
        protected Label labExamTime;
        protected Label labR;
        protected Label labUserCode;
        protected Label labUserName;
        protected Label labW;
        protected Panel PanelShow;
        private PublicFun publicFun_0 = new PublicFun();
        protected HtmlInputHidden StartTime;
        protected string strEndTime = null;
        private string string_0 = "";
        private string string_1 = "";
        protected string strStartTime = null;
        protected string strSumTime = null;
        protected HtmlInputHidden SumTime;
        protected HtmlInputHidden txtHideComm;
        protected HtmlInputHidden txtHideEnter;
        protected TextBox txtWriteDoc;

        private void method_0()
        {
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter("select RubricDoc from ZiRubricInfo where Flag='" + this.string_0.Trim() + "'", selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "RubricInfo");
            int count = dataSet.Tables["RubricInfo"].Rows.Count;
            int num2 = new Random().Next(count);
            this.labDoc.Text = dataSet.Tables["RubricInfo"].Rows[num2]["RubricDoc"].ToString().Trim();
            adapter.Dispose();
            dataSet.Dispose();
            selectConnection.Dispose();
            Convert.ToInt32(this.string_1.Trim());
            this.SumTime.Value = this.string_1.ToString().Trim();
            this.StartTime.Value = DateTime.Now.ToLongTimeString();
            string text1 = this.StartTime.Value;
            this.EndTime.Value = DateTime.Now.AddMinutes(Convert.ToDouble(this.SumTime.Value)).ToLongTimeString();
        }

        private void method_1(string string_2)
        {
            HtmlTable child = new HtmlTable {
                ID = this.httpCookie_0.Value,
                Width = "100%",
                Border = 0,
                Align = "left",
                CellSpacing = 0,
                CellPadding = 0,
                BorderColor = "#ffffff"
            };
            child.Style.Add("BORDER-COLLAPSE", "collapse");
            int num = 0;
            if ((string_2.Length % 40) != 0)
            {
                num = Convert.ToInt32((int) (string_2.Length / 40)) + 1;
            }
            else
            {
                num = string_2.Length / 40;
            }
            for (int i = 0; i < num; i++)
            {
                HtmlTableRow row = new HtmlTableRow();
                for (int j = 0; j < 40; j++)
                {
                    if (string_2.Length > ((i * 40) + j))
                    {
                        HtmlTableCell cell = new HtmlTableCell {
                            Width = "20",
                            Height = "20"
                        };
                        TextBox box = new TextBox {
                            ID = "txtS" + Convert.ToString((int) ((i * 40) + j)),
                            Text = string_2.Substring((i * 40) + j, 1),
                            ForeColor = Color.Black,
                            Width = 20,
                            Height = 20,
                            ReadOnly = true,
                            BackColor = Color.WhiteSmoke
                        };
                        box.Style.Add("BORDER-RIGHT", "#ffffff 1px solid");
                        box.Style.Add("BORDER-TOP", "#ffffff 1px solid");
                        box.Style.Add("BORDER-LEFT", "#ffffff 1px solid");
                        box.Style.Add("BORDER-BOTTOM", "#ffffff 1px solid");
                        box.Style.Add("FONT-SIZE", "16px");
                        cell.Controls.Add(box);
                        row.Cells.Add(cell);
                    }
                }
                child.Rows.Add(row);
            }
            this.PanelShow.Controls.Add(child);
        }

        private void method_2()
        {
            string str = DateTime.Now.ToShortDateString();
            string str2 = DateTime.Now.ToShortDateString();
            string str3 = "1";
            string str4 = "0";
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("select * from ZiSetInfo where JoinUserCode like '%," + this.httpCookie_0.Value + ",%'", connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.Read())
            {
                str = reader["ExamStart"].ToString();
                str2 = reader["ExamEnd"].ToString();
                str3 = reader["JoinNum"].ToString();
                str4 = "1";
            }
            reader.Close();
            command.Dispose();
            connection.Close();
            connection.Dispose();
            if (str4 == "1")
            {
                string values = this.publicFun_0.GetValues("select Count(*) as CountInfo from ZiGradeInfo where ExamType='A' and UserCode='" + this.httpCookie_0.Value + "' and (Convert(datetime,ExamDate)>=Convert(dateTime,'" + str + "') and Convert(datetime,ExamDate)<=Convert(dateTime,'" + str2 + "'))", "CountInfo");
                if (values.Trim() == "")
                {
                    values = "0";
                }
                if (Convert.ToInt16(str3) <= Convert.ToInt16(values))
                {
                    base.Response.Write("<script>alert('考核次数已超出，无法进入考场！');</script>");
                }
                else if (this.publicFun_0.GetValues("select ExamTime from ZiSetInfo where getdate()>=Convert(datetime,ExamStart) and Convert(datetime,ExamEnd)>=getdate()", "ExamTime") == "")
                {
                    base.Response.Write("<script>alert('考核有效时间过期或未到，无法进入考场！');</script>");
                }
            }
            else
            {
                base.Response.Write("<script>alert('您暂未纳入考试名单中，所以无法进入考场！');</script>");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            this.httpCookie_4 = new HttpCookie("ExamDeptName");
            this.httpCookie_4 = base.Request.Cookies["ExamDeptName"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                this.int_0 = Convert.ToInt32(base.Request["ID"].ToString());
                this.string_0 = base.Request["Type"].ToString();
                this.string_1 = base.Request["Time"].ToString();
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                this.httpCookie_4.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_4.Value));
                if (base.IsPostBack)
                {
                    string text = "";
                    string str3 = this.labDoc.Text.Trim();
                    if (this.txtHideComm.Value.Trim() == "EndExam")
                    {
                        int num = 0;
                        double num3 = 0.0;
                        try
                        {
                            try
                            {
                                text = this.txtWriteDoc.Text;
                                if (text.Trim().Length <= 0)
                                {
                                    this.publicFun_0.ExecuteSql(string.Concat(new object[] { "update ZiGradeInfo set FenValue=", num, ",IsPass='Z' where ID=", this.int_0 }));
                                    base.Response.Write("<script>alert('很遗憾,打字考试未能通过(0/分钟,总得分：0),正确率：0%) ！');window.close();</script>");
                                }
                                else
                                {
                                    if (text.Trim().Length > str3.Trim().Length)
                                    {
                                        text = text.Trim().Substring(0, str3.Trim().Length - 1);
                                    }
                                    string values = this.publicFun_0.GetValues("select ExamTime from ZiSetInfo", "ExamTime");
                                    if (values == "")
                                    {
                                        values = "5";
                                    }
                                    double num5 = Convert.ToDouble(values);
                                    for (int i = 0; i < text.Trim().Length; i++)
                                    {
                                        str3.Trim().Substring(i, 1);
                                        text.Trim().Substring(i, 1);
                                        if (str3.Trim().Substring(i, 1) == text.Trim().Substring(i, 1))
                                        {
                                            num3++;
                                        }
                                    }
                                    if (!(num3 == 0.0))
                                    {
                                        num = Convert.ToInt16((double) (num3 / num5));
                                    }
                                    else
                                    {
                                        num = 0;
                                    }
                                    int length = this.txtWriteDoc.Text.Length;
                                    double num2 = 0.0;
                                    if (!((length != 0) ? (num3 == 0.0) : true))
                                    {
                                        num2 = (num3 / ((double) length)) * 100.0;
                                    }
                                    num2 = Convert.ToDouble(num2.ToString("0.00"));
                                    string str2 = this.publicFun_0.GetValues("select PassNum from ZiStandInfo where " + this.ViewState["AgeInfo"].ToString() + ">=AgeStart and AgeEnd>=" + this.ViewState["AgeInfo"].ToString() + " and Worker='" + this.ViewState["WorkerInfo"].ToString() + "'", "PassNum");
                                    if (str2.Trim() == "")
                                    {
                                        this.publicFun_0.ExecuteSql("update ZiGradeInfo set IsPass='Z' where ID=" + this.int_0);
                                        base.Response.Write("<script>alert('很遗憾,考核标准未设置,无法得知考试结果,正确率：" + num2 + "%！');window.close();</script>");
                                    }
                                    else if (num < Convert.ToInt16(str2))
                                    {
                                        this.publicFun_0.ExecuteSql(string.Concat(new object[] { "update ZiGradeInfo set RightNum='", num2, "',FenValue=", num, ",IsPass='N' where ID=", this.int_0 }));
                                        base.Response.Write(string.Concat(new object[] { "<script>alert('很遗憾,打字考试未能通过(", num.ToString(), "/分钟,正确率：", num2, "%) ！');window.close();</script>" }));
                                    }
                                    else
                                    {
                                        this.publicFun_0.ExecuteSql(string.Concat(new object[] { "update ZiGradeInfo set RightNum='", num2, "',FenValue=", num, ",IsPass='Y' where ID=", this.int_0 }));
                                        base.Response.Write(string.Concat(new object[] { "<script>alert('恭喜,打字考试顺利通过(", num.ToString(), "/分钟,正确率：", num2, "%) ！');window.close();</script>" }));
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                        finally
                        {
                        }
                    }
                }
                else
                {
                    this.ViewState["LoginTime"] = DateTime.Now.ToString();
                    this.labUserCode.Text = this.httpCookie_0.Value;
                    this.labUserName.Text = this.httpCookie_1.Value;
                    this.labDeptName.Text = this.httpCookie_4.Value;
                    if (this.labDeptName.Text.Trim().Length > 20)
                    {
                        this.labDeptName.Text = this.labDeptName.Text.Substring(0, 20) + "…";
                    }
                    this.labExamTime.Text = this.string_1;
                    string str6 = this.publicFun_0.GetValues("select UserPhoto from UserInfo where UserCode='" + this.httpCookie_0.Value + "'", "UserPhoto").Trim();
                    if (str6 != "")
                    {
                        this.ImageUser.ImageUrl = "../PublicFile/" + str6;
                    }
                    if (this.labExamTime.Text.Trim() != "0")
                    {
                        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection.Open();
                        SqlDataReader reader = new SqlCommand("select UserCode,UserName,Worker,AgeInfo from ZiGradeInfo where ID=" + this.int_0, connection).ExecuteReader(CommandBehavior.CloseConnection);
                        if (reader.Read())
                        {
                            this.ViewState["AgeInfo"] = reader["AgeInfo"].ToString();
                            this.ViewState["WorkerInfo"] = reader["Worker"].ToString();
                        }
                        reader.Close();
                        connection.Close();
                        connection.Dispose();
                        if (this.string_0 == "A")
                        {
                            this.method_2();
                        }
                        this.method_0();
                        this.method_1(this.labDoc.Text.Trim());
                        this.txtWriteDoc.Attributes.Add("onpropertychange", "javascript:WriteDocOK();");
                        this.txtWriteDoc.Attributes.Add("oninput", "javascript:WriteDocOK();");
                    }
                    else
                    {
                        base.Response.Write("<script>alert('没有找到相关试卷参数！');</script>");
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

